package com.zj.common.mq;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.DynamicDestinationResolver;

import com.ibm.mq.jms.MQDestination;
import com.ibm.msg.client.wmq.compat.jms.internal.JMSC;


/**
 * 
 * Dynamic destination resolver which can set some IBM WebSphere MQ
 * specific properties.
 * 
 * @author <a href="mailto:david@davidkarlsen.com">David J. M. Karlsen</a>
 * @see com.ibm.mq.jms.JMSC
 * @see DestinationResolver
 * @see DynamicDestinationResolver
 *
 */

public class IBMWebSphereMqDestinationResolver 
    extends DynamicDestinationResolver
    implements DestinationResolver
{
    private int ccsid = 819;
    private int encoding = JMSC.MQJMS_ENCODING_NATIVE;
    private int failIfQuiesce = JMSC.MQJMS_FIQ_YES;
    private int targetClient = JMSC.MQJMS_CLIENT_NONJMS_MQ;
   
    /**
     * @param ccsid
     * Character set for automatic conversion of message payload.
     * <p>
     * Defaults to com.ibm.mq.jms.JMSC.MQJMS_FIQ_YES
     */
    public void setCcsid( int ccsid )
    {
        this.ccsid = ccsid;
    }
    /**
     * @param encoding
     * Encoding for message payload.
     * <p>
     * Defaults to com.ibm.mq.jms.JMSC.MQJMS_ENCODING_NATIVE
     */
    public void setEncoding( int encoding )
    {
        this.encoding = encoding;
    }
    /**
     * @param failIfQuiesce
     * 
     * Control graceful exits when queue manager is quesing.
     * <p>
     * Defaults to com.ibm.mq.jms.JMSC.MQJMS_FIQ_YES
     */
    public void setFailIfQuiesce( int failIfQuiesce )
    {
        this.failIfQuiesce = failIfQuiesce;
    }
    /**
     * @param targetClient
     * Control target client type (JMS compliant or not).
     * <p>
     * Defaults to com.ibm.mq.jms.JMSC.MQJMS_CLIENT_JMS_COMPLIANT
     */
    public void setTargetClient( int targetClient )
    {
        this.targetClient = targetClient;
    }
    protected int getCcsid()
    {
        return ccsid;
    }
    protected int getEncoding()
    {
        return encoding;
    }
    protected int getFailIfQuiesce()
    {
        return failIfQuiesce;
    }
    protected int getTargetClient()
    {
        return targetClient;
    }
    
    /**
     * {@inheritDoc}
     * @see DestinationResolver#resolveDestinationName(Session, String, boolean)
     */
    public Destination resolveDestinationName( Session session, 
                                               String destinationName, 
                                               boolean isPubSubDomain ) 
        throws JMSException
    {

        Destination destination = super.resolveDestinationName( session, destinationName, isPubSubDomain );
        if ( destination instanceof MQDestination )
        {
            MQDestination mqDestination = (MQDestination) destination;
            mqDestination.setCCSID( getCcsid() );
            mqDestination.setEncoding( getEncoding() );
            mqDestination.setFailIfQuiesce( getFailIfQuiesce() );
            mqDestination.setTargetClient( getTargetClient() );
        }
        
        return destination;
    }

}
